﻿@model BulkEditListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = T("Admin.Catalog.BulkEdit").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/subsites/Administration/Content/images/ico-catalog.png")" alt="" />
            @T("Admin.Catalog.BulkEdit")
        </div>
        <div class="options">
        </div>
    </div>

    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.SearchProductName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
            </td>
        </tr>
        @if (Model.IsStoreAdminOrAdmin)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.SearchRegionId):
                </td>
                <td class="adminData">
                    @(Html.DropDownList("SearchRegionId", Model.AvailableRegions))
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchManufacturerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchStoreId):
            </td>
            <td class="adminData">
                @(Html.Telerik().ComboBoxFor(model => model.SearchStoreId)
                        .Value(Model.SearchStoreId == 0 ? string.Empty : Model.SearchStoreId.ToString())
                        .AutoFill(true)
                        .Filterable()
                        .DataBinding(binding => binding.Ajax().Select("StoreList", "Store", new { storeId = Model.SearchStoreId, customerId = 0 })))
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-products" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<BulkEditProductVariantModel>()
                        .Name("productvariants-grid")
                        .DataKeys(keys =>
                        {
                            keys.Add(pv => pv.Id);
                        })
                        .ToolBar(commands =>
                        {
                            commands.SubmitChanges();
                        })
                        .DataBinding(dataBinding =>
                            dataBinding.Ajax()
                                .Select("BulkEditSelect", "ProductVariant")
                                .Update("BulkEditSave", "ProductVariant")
                        )
                        .Columns(columns =>
                        {
                            columns.Bound(pv => pv.Name).ReadOnly().Width(300);
                            columns.Bound(pv => pv.Sku).Width(100);
                            columns.Bound(pv => pv.Price).Width(100);
                            columns.Bound(pv => pv.OldPrice).Width(100);
                            columns.Bound(pv => pv.ManageInventoryMethod).ReadOnly().Width(150);
                            if (Model.IsStoreAdminOrAdmin)
                            {
                                columns.Bound(pv => pv.StockQuantity).Width(100);
                            }
                            else
                            {
                                columns.Bound(pv => pv.StockQuantity).ReadOnly().Width(100);
                            }
                            columns.Bound(pv => pv.Published).Width(100);
                            columns.Command(commands => commands.Delete().Text(T("Admin.Common.Delete").Text)).Title(T("Admin.Common.Delete").Text).Width(50);
                            columns.Bound(x => x.Id)
                                .ReadOnly()
                                .Width(50)
                                .Centered()
                                .Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", new { id = x.Id }))
                                .ClientTemplate("<a href=\"Edit/<#= Id #>\">" + T("Admin.Common.View").Text + "</a>")
                                .Title(T("Admin.Common.View").Text);
                        })
                        .ClientEvents(events => events.OnDataBinding("Grid_onDataBinding").OnError("Grid_onError").OnSubmitChanges("Grid_onSubmitChanges"))
                        .Editable(editing => editing.Mode(GridEditMode.InCell))
                        .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both)))

                <script type="text/javascript">

                    $(document).ready(function () {
                        $('#search-products').click(function () {
                            var grid = $('#productvariants-grid').data('tGrid');
                            grid.currentPage = 1; //new search. Set page size to 1
                            grid.ajaxRequest();
                            return false;
                        });

                        $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                            if (event.keyCode == 13) {
                                $("#search-products").click();
                                return false;
                            }
                        });
                    });

                    function Grid_onError(args) {
                        if (args.textStatus == "modelstateerror" && args.modelState) {
                            var message = "Errors:\n";
                            $.each(args.modelState, function (key, value) {
                                if ('errors' in value) {
                                    $.each(value.errors, function () {
                                        message += this + "\n";
                                    });
                                }
                            });
                            args.preventDefault();
                            alert(message);
                        }
                    }
                    function Grid_onDataBinding(e) {
                        var loadData = true;
                        var grid = $(this).data('tGrid');
                        if (grid.hasChanges()) {
                            if (!confirm('@T("Admin.Common.LoseUnsavedChanges")')) {
                                e.preventDefault();
                                loadData = false;
                            }
                        }
                        if (loadData) {
                            var searchModel = {
                                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                                SearchCategoryId: $('#SearchCategoryId').val(),
                                SearchManufacturerId: $('#SearchManufacturerId').val(),
                                SearchStoreId: $('#SearchStoreId').val(),
                                SearchRegionId: $('#SearchRegionId').val()
                            };
                            e.data = searchModel;
                        }
                    }
                    function Grid_onSubmitChanges(e) {
                        //Pass search parameters

                        e.values["SearchProductName"] = $('#@Html.FieldIdFor(model => model.SearchProductName)').val();
                        e.values["SearchCategoryId"] = $('#SearchCategoryId').val();
                        e.values["SearchManufacturerId"] = $('#SearchManufacturerId').val();
                        e.values["SearchStoreId"] = $('#@Html.FieldIdFor(model => model.SearchStoreId)').val();
                        e.values["SearchRegionId"] = $('#@Html.FieldIdFor(model => model.SearchRegionId)').val();
                    }
                </script>
            </td>
        </tr>
    </table>
}